
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.PosterDao;
import com.sun.showcase.biz.mall.service.PosterService;
import com.sun.showcase.client.domain.mall.Poster;
import com.sun.showcase.client.query.mall.PosterQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("posterService")
@Transactional
public class PosterServiceImpl implements PosterService{
	@Autowired
	private PosterDao posterDao;
	
	public void setPosterDao(PosterDao dao) {
		this.posterDao = dao;
	}

	public DataGrid datagrid(PosterQuery posterQuery) {
		DataGrid j = new DataGrid();
		Pager<Poster> pager  = find(posterQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<PosterQuery> getQuerysFromEntitys(List<Poster> posters) {
		List<PosterQuery> posterQuerys = new ArrayList<PosterQuery>();
		if (posters != null && posters.size() > 0) {
			for (Poster tb : posters) {
				PosterQuery b = new PosterQuery();
				BeanUtils.copyProperties(tb, b);
				posterQuerys.add(b);
			}
		}
		return posterQuerys;
	}

	private Pager<Poster> find(PosterQuery posterQuery) {
		Page<Poster> page = PageHelper.startPage(posterQuery.getPage().intValue(), posterQuery.getRows().intValue());
		List<Poster> list = posterDao.findList(posterQuery);
		Pager<Poster> pager = new Pager<Poster>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Poster add(PosterQuery posterQuery) {
		Poster t = new Poster();
		BeanUtils.copyProperties(posterQuery, t);
		posterDao.save(t);
		posterQuery.setId(t.getId());
		return t;
		
	}

	public void update(PosterQuery posterQuery) {
		Poster t = posterDao.getById(posterQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(posterQuery, t);
	    	posterDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(PosterQuery posterQuery) {
	    posterDao.updatePart(posterQuery);
	}

	public void delete(String[] ids) {
		posterDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		posterDao.deletePt(ids);
	}

	public Poster get(PosterQuery posterQuery) {
		return posterDao.getById(posterQuery.getId());
	}

	public Poster get(String id) {
		return posterDao.getById(id);
	}

	
	public List<PosterQuery> listAll(PosterQuery posterQuery) {
	    List<Poster> list = posterDao.findList(posterQuery);
		List<PosterQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public Poster get() {
		Poster p = this.get("1");
		if(p==null) {
			PosterQuery posterQuery = new PosterQuery();
			posterQuery.setBckImage("http://a.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1fc9c7b6853a4e251f94ca5f46.jpg");
			posterQuery.setTitle("加入我们,有钱赚！");
			posterQuery.setTitleTwo("扫描二维码");
			posterQuery.setTitleThree("和我一起赚钱");
			posterQuery.setNameColor("#000000");
			posterQuery.setTitleColor("#FFFFFF");
			posterQuery.setId("1");
			p = this.add(posterQuery);
		}
		return p;
	}
	
	
}
